package com.frame.work.frame.deal.device0fbe;

import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.frame.deal.base.BaseFrame;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import static com.frame.work.common.constant.CallType.TYPE_GET;
import static com.frame.work.common.constant.CallType.TYPE_SET;
/**
 *魔幻开关
 */
@Data
public class MoHuanKaiGuan_0FBE0802 extends BaseFrame {

    private Integer switchState;

    public MoHuanKaiGuan_0FBE0802(String gatewayMac, String data,String type) {
        //将基础数据进行处理
        super(gatewayMac, data);
        //根据不同类型进行判断
        if(TYPE_GET.equals(type)){
            MoHuanKaiGuan_0FBE0802Get(gatewayMac,data);
        }else if (TYPE_SET.equals(type)){
            MoHuanKaiGuan_0FBE0802Set(gatewayMac,data);
        }
    }


    public void MoHuanKaiGuan_0FBE0802Get(String gatewayMac, String data) {


        //处理此设备特有的数据
        String after0FAA = StringUtils.substring(data, 22, 24);
        String dianya = StringUtils.substring(data, data.length() - 8, data.length() - 4);
        float dianyaNumber1 = Float.parseFloat(StringUtils.substring(dianya, 2, 3) + '.' + StringUtils.substring(dianya, 3, 4) + StringUtils.substring(dianya, 0, 2));
        DecimalFormat df = new DecimalFormat(".0");
        String dianyaNumber = df.format(dianyaNumber1);

        this.setSwitchState(-100);
        System.out.println(after0FAA);
        if ("FF".equals(after0FAA)) {

            String sceneNumber = StringUtils.substring(data, data.length() - 2, data.length());

            //将actions信息放入json对象中
            Map<String, Object> actions = new HashMap();
            actions.put("dianya", dianyaNumber + "");
            actions.put("sceneNumber", sceneNumber);
            this.setActions(actions);


            //处理好的json对象
            String json = JSONObject.toJSONString(this);
            //将数据发送到mqtt队列中
            JsonToMqtt.sendMQTT(gatewayMac, json);

        }


    }


    public void MoHuanKaiGuan_0FBE0802Set(String gatewayMac, String data) {

        this.setOdIndex("0FBE");
        this.setDeviceType("08");
        this.setProductType("02");
        this.setSwitchState(-100);

        //处理此设备特有的数据
        String afterOdIndex = StringUtils.substring(data, 22, 24);
        String state = StringUtils.substring(data, 24, 26);

        if ("01".equals(afterOdIndex)) {


            //将actions信息放入json对象中
            Map<String, Object> actions = new HashMap();
            actions.put("wayNumber", 1);
            actions.put("state", state);

            this.setActions(actions);


            //处理好的json对象
            String json = JSONObject.toJSONString(this);
            //将数据发送到mqtt队列中
            JsonToMqtt.sendMQTT(gatewayMac, json);

        } else if ("02".equals(afterOdIndex)) {

            //将actions信息放入json对象中
            Map<String, Object> actions = new HashMap();
            actions.put("wayNumber", 2);
            actions.put("state", state);

            this.setActions(actions);


            //处理好的json对象
            String json = JSONObject.toJSONString(this);
            //将数据发送到mqtt队列中
            JsonToMqtt.sendMQTT(gatewayMac, json);

        } else if ("03".equals(afterOdIndex)) {

            //将actions信息放入json对象中
            Map<String, Object> actions = new HashMap();
            actions.put("wayNumber", 3);
            actions.put("state", state);

            this.setActions(actions);


            //处理好的json对象
            String json = JSONObject.toJSONString(this);
            //将数据发送到mqtt队列中
            JsonToMqtt.sendMQTT(gatewayMac, json);


        }


    }

}
